package spider;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * 爬取知乎用户信息
 */
public class userSpider {
  //https://www.zhihu.com/people/elyn
  //随机用户地址


  public static void getUserInfo() throws IOException {
    String html = proxySpider.getIndex("https://www.zhihu.com/people/gte-18");

    Document doc = Jsoup.parse(html);

    //用户名
//        Elements trs = doc.select("#ProfileHeader > div > div.ProfileHeader-wrapper > div > div.ProfileHeader-content > div.ProfileHeader-contentHead > h1 > span.ProfileHeader-name");
//        System.out.println(trs);

    //简介
//        Elements jj=doc.select("#ProfileHeader > div > div.ProfileHeader-wrapper > div > div.ProfileHeader-content > div.ProfileHeader-contentHead > h1 > span.ztext.ProfileHeader-headline");
//        System.out.println(jj);

    //回答数,视频，提问，文章，专栏，想法
//        Elements An=doc.select("#ProfileMain > div.ProfileMain-header > ul > li > a > span");
//        System.out.println(An);

    //关注数,被关注数
//        Elements follow=doc.select("#root > div > main > div > div.Profile-main > div.Profile-sideColumn > div.Card.FollowshipCard > div > a > div > strong");
//        System.out.println(follow);

    //关注话题，关注专栏，关注问题，关注收藏夹
//        Elements follow2=doc.select("#root > div > main > div > div.Profile-main > div.Profile-sideColumn > div.Profile-lightList > a > span.Profile-lightItemValue");
//        System.out.println(follow2);

    //基本信息都有，但是需要提取
    Elements xx = doc.select("#root > div > main > div > meta");
    System.out.println(xx);
  }

  public static void getUserFollow() throws IOException {
    int offset = 20;

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClients.custom()
        .setRetryHandler(new DefaultHttpRequestRetryHandler())
        .setConnectionManager(cm)
        .build();
    HttpGet req = new HttpGet(
        "https://www.zhihu.com/api/v4/members/gte-18/followees?include=data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics&offset="
            + offset + "&limit=20");
    req.setHeader("referer", "https://www.zhihu.com/people/gte-18/following?page=1");
    req.setHeader("X-Requested-With", "fetch");
//        req.setHeader("Host","https://www.zhihu.com");
//        req.setHeader("Origin","https://www.zhihu.com");
    req.setHeader("Referrer Policy", "no-referrer-when-downgrade");

    req.setHeader("authorization", "oauth c3cef7c66a1843f8b3a9e6a1e3160e20");
    req.setHeader(
        "User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36");

    HttpResponse resp = httpClient.execute(req);
    String result = EntityUtils.toString(resp.getEntity());

    Document doc = Jsoup.parse(result);
//        String sb = EntityUtils.toString(resp.getEntity());
//            if (sb.length() < 25) break;
    System.out.println(doc);

    Elements elements = doc.select("#Profile-following > div:nth-child(2) > div");

//        System.out.println(elements);

//        for(Element element:elements){
//            System.out.println(element);
////            System.out.println(element.select("#Popover11-toggle > a"));
//        }
//            while (m.find() && m1.find()){
//                String topicid=m1.group().substring(16, m1.group().length() - 2);
//                String temp = m.group().substring(8, m.group().length() - 10);
//                String topicName=Spider.topicSpider.unicodeStr2String(temp);
//
//                System.out.println(topicid + " " + topicName);
    //转义单引号和双引号
//                topicName=topicName.replaceAll("'|\"","\\\\'");
//                String sql ="INSERT INTO `topic`(`topicId`, `topicName`, `parentId`) VALUES ('"+topicid+"','"+topicName+"','"+parentId+"')";
//                statement.execute(sql);//执行语句
//            }
//            offset += 20;
//        }
  }
}
